#!/bin/bash

SHELL_DIR="$(cd $(dirname $0); pwd)"
PROJECT_DIR=$1
SYSTEM_DIR=$2
system_folder_name=`ls *system* -d`
if [[ ! -d ${SYSTEM_DIR} ]]; then
	echo "无SYSTEM目录，爪巴！"
	exit 1
fi
#echo "当前项目目工作录 :$PROJECT_DIR" 
#echo "插件目录 : $SHELL_DIR"
#echo "当前项目刷机包的system目录 :$SYSTEM_DIR"
#echo -e "\e[36m      >>>>>>>>>>     .     >>>>>>>>>> \e[0m"
echo ""
echo ""

if [[ -f $SHELL_DIR/Insides/apktool/apktool.jar ]]; then
	ApkTool="java -jar $SHELL_DIR/Insides/apktool/apktool.jar" 
	echo "ApkTool version:" && $ApkTool -version
	
else
	echo -e "\e[36m     >>>>>>>>>   没有找到apktool   >>>>>>>> \e[0m"
	echo "   ----->请在$SHELL_DIR/Insides/apktool目录下查看是否有apktool "
	sleep 10
	exit 1
fi

if [ -d $SHELL_DIR/MiuiSystemUI ]; then

	${su} rm -rf $SHELL_DIR/MiuiSystemUI
fi

lin_dir=${PROJECT_DIR##*/}
if [ ! -d $SHELL_DIR/$lin_dir  ];then
  mkdir $SHELL_DIR/$lin_dir
fi

if [[ -f $PROJECT_DIR/system_ext/priv-app/MiuiSystemUI/MiuiSystemUI.apk ]]; then
	echo -e "\e[36m      >>>>>>>>>>>   开始尝试进行网速秒刷新     >>>>>>>>>>> \e[0m"
	echo ""
	echo ""
	cp $PROJECT_DIR/system_ext/priv-app/MiuiSystemUI/MiuiSystemUI.apk $SHELL_DIR/$lin_dir/MiuiSystemUI.apk.bak
		
	$ApkTool d -q -r -f -o $SHELL_DIR/MiuiSystemUI $PROJECT_DIR/system_ext/priv-app/MiuiSystemUI/MiuiSystemUI.apk		

	if [ -d $SHELL_DIR/MiuiSystemUI ]; then
		echo -e "\e[36m    ------------->开始更改网速秒刷新    \e[0m"
		echo ""
		echo ""
		sed -i 's/const-wide\/16 v0, 0xfa0/const-wide\/16 v0, 0x3e8/g' $SHELL_DIR/MiuiSystemUI/smali_classes2/com/android/systemui/statusbar/policy/NetworkSpeedController.smali

		
		
		$ApkTool b -q -f -o $PROJECT_DIR/system_ext/priv-app/MiuiSystemUI/MiuiSystemUI.apk $SHELL_DIR/MiuiSystemUI
		${su} rm -rf $SHELL_DIR/MiuiSystemUI
		echo -e "\e[36m        网速秒刷新更改成功<-----------------    \e[0m"
		sleep 3
		exit 0
	fi
	echo -e "\e[36m        网速秒刷新更改失败<-----------------    \e[0m"	
	sleep 3
	exit 1	
					
fi	

echo -e "\e[36m        ----------->找不到相关APP<--------------    \e[0m"	
echo -e "\e[36m        ----------->找不到相关APP<--------------    \e[0m"
echo -e "\e[36m        ----------->找不到相关APP<--------------    \e[0m"
sleep 3


#chmod -R 0777 $PROJECT_DIR
echo -e "\e[32mDone\e[0m"


